﻿using System;
using System.Collections.Generic;
using XCI.Sys.Model;
using XCI.Core;
using XCI.Windows.Extensions;
using XCI.Helper;

namespace XCI.Sys.Service
{
    /// <summary>
    /// 系统在线用户服务
    /// </summary>
    public class OnlineUserService : BaseService<OnlineUserModel>
    {
        public static readonly OnlineUserService Instance = new OnlineUserService();
        protected override string RootUrl => "/api/sys/onlineUser/";

        /// <summary>
        /// 激活在线用户
        /// </summary>
        /// <param name="userId">用户主键</param>
        public bool Active(string userId)
        {
            try
            {
                var url = GetUrl("active", userId);
                return SysRuntime.Api.PostAsMessage(url).EnsureSuccess().Success;
            }
            catch (Exception e)
            {
                DebugHelper.Catch(DebugHelper.GetExceptionAllMessage(e));
                return false;
            }
        }

        /// <summary>
        /// 注销在线用户
        /// </summary>
        /// <param name="userId">用户主键</param>
        public bool Logoff(string userId)
        {
            var url = GetUrl("logoff", userId);
            return SysRuntime.Api.PostAsMessage(url).EnsureSuccess().Success;
        }

        /// <summary>
        /// 查询单个在线用户
        /// </summary>
        /// <param name="userId">用户主键</param>
        public OnlineUserModel Get(string userId)
        {
            var url = GetUrl("get", userId);
            return SysRuntime.Api.Post<OnlineUserModel>(url).EnsureSuccess().Data;
        }

        /// <summary>
        /// 查询在线用户列表
        /// </summary>
        public List<OnlineUserModel> Query()
        {
            var url = GetUrl("query");
            return SysRuntime.Api.Post<List<OnlineUserModel>>(url).EnsureSuccess().Data;
        }

        /// <summary>
        /// 导出在线用户列表
        /// </summary>
        public byte[] Export()
        {
            return ExportCore();
        }
    }
}
